<?php

require_once 'Smarty.class.php';
require_once 'Smarty-Config.class.php';

class SmartyStart extends Smarty{

    public $url = null;

    public function  __construct($url = null) {
        parent::__construct();

        $this->debugging       = SmartyConfig::$debug;
        $this->left_delimiter  = SmartyConfig::$leftTag;
        $this->right_delimiter = SmartyConfig::$rigthTag;
        $this->template_dir    = SmartyConfig::$tmpDir;
        $this->compile_dir     = SmartyConfig::$compDir;
        $this->config_dir      = SmartyConfig::$confDir;
        $this->cache_dir       = SmartyConfig::$cacheDir;

        $this->url = $url;
    }

    /**
     * 
     * @param <type> $config
     * @param <type> $cols
     * @param <type> $primaryKey
     * @param <type> $data
     * @param <type> $search
     * @param <type> $pagination
     * @param <string> $table
     * @param <type> $tmp
     * @return <type> 
     */
    public function renderGrid($config, $cols, $primaryKey, $data, $search = null, $pagination = null, $table = null, $tmp = "grid.tpl"){
        
        if($table == null){
            $table = array("border"=>"0", "width"=>"100%", "cellp"=>"2", "cells"=>"2");
        }

        $this->assign('grid', $config);
        $this->assign('fieldId', $primaryKey);
        $this->assign('columns', $cols);
        $this->assign('rows', $data);

        if($search != null){
            $this->assign('search', $search);
        }

        if($pagination != null){
            $this->assign('paginacao', $pagination);
            $this->assign('pgn', $_POST['pgn']);
        }
                
        $this->assign('table', $table);

        return $this->fetch($tmp);
    }

    /**
     * 
     * @param <type> $config
     * @param <type> $action
     * @param <type> $fields
     * @param <type> $butons
     * @param <type> $values
     * @param <type> $tmp
     * @return <type>
     */
    public function renderForm($config, $action, $fields, $buttons, $values = null, $tmp = "form.tpl"){
        $this->assign('form', $config);
        $this->assign('action', $action);
        $this->assign('fields', $fields);
        $this->assign('buttons', $buttons);

        if($values != null){
            $this->assign('values', $values);
        }

        return $this->fetch($tmp);
    }

    /**
     *
     * @param <type> $content
     * @param <type> $title
     * @param <type> $menu
     * @param <type> $tmp
     * @return <type> 
     */
    public function renderLayout($content,$title,$menu,$tmp = "layout.tpl"){
        $this->assign('title', $title);
        $this->assign('menu', $menu);
        $this->assign('workarea', $content);
        $this->assign('host', $this->url);
        $this->assign('sessao', $_SESSION['LOG_USUARIO'] . " - " . date("d/m/Y H:i:s"));
        $this->assign('qtd_sms', $_SESSION['QTD_SMS']);

        return $this->fetch($tmp);
    }

    public function renderMenu(){
        require_once 'Smarty-TreeMenu.class.php';
        $treemenu = new TreeMenu($this->url);

        return $treemenu->getTreeMenu();
    }

    public function renderLogin($title, $action, $url = "", $tmp = "login.tpl"){
        $this->assign('title', $title);
        $this->assign('url', $url);
        $this->assign('action', $action);

        return $this->display($tmp);
    }

    public function renderGridAjax($tmp,$param,$url,$title){
        $this->assign('host', $url);
        $this->assign('cod', $param);
        $this->assign('title', $title);
        $this->assign('sessao', $_SESSION['LOG_USUARIO'] . " - " . date("d/m/Y H:i:s"));

        return $this->fetch($tmp);
    }
}